<!-- 搜索表单 -->
<template>
  <div>
    <el-form label-width="77px"
             class="ele-form-search"
             ref="where"
             :model="where"
             @keyup.enter.native="search"
             @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :span="24">
          <h5 class="navList">进件列表</h5>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { lg: 8, md: 12 } : { span: 8 }">
          <el-form-item label="商家名称:">
            <el-input clearable
                      v-model="where.merchantName"
                      placeholder="请输商家名称" />
          </el-form-item>
        </el-col>

        <el-col v-bind="styleResponsive ? { lg: 8, md: 12 } : { span: 8 }">
          <el-form-item label="所属代理:">
            <el-select clearable
                       v-model="where.oemName"
                       v-loadmore="loadMore"
                       placeholder="请选择所属代理"
                       @visible-change="oemNameFn"
                       class="ele-fluid">
              <el-option v-for="(i, j) in subordinateAgentList"
                         :key="j"
                         :label="i.label"
                         :value="i.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { lg: 8, md: 12 } : { span: 8 }">
          <el-form-item label="进件状态:">
            <el-select clearable
                       v-model="where.merchanApplyForStatus"
                       placeholder="请选择进件状态"
                       class="ele-fluid">
              <el-option v-for="(i, j) in typeList"
                         :key="j"
                         :label="i.label"
                         :value="i.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col v-bind="styleResponsive ? { lg: 8, md: 12 } : { span: 8 }">
          <el-form-item label="申请时间:">
            <el-date-picker v-model="where.dateRange"
                            type="datetimerange"
                            unlink-panels
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            class="ele-fluid" />
          </el-form-item>
        </el-col>

        <el-col v-bind="styleResponsive ? { lg: 8, md: 12 } : { span: 8 }">
          <el-form-item label="进件类型:">
            <el-select clearable
                       v-model="where.applyForType"
                       placeholder="请选择"
                       class="ele-fluid">
              <el-option label="微信"
                         :value="1" />
              <el-option label="支付宝"
                         :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <div class="ele-form-actions">
            <el-button type="primary"
                       icon="el-icon-search"
                       class="ele-btn-icon"
                       @click="search">
              查询
            </el-button>
            <el-button @click="resetForm('where')">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { oemQueryAgentList } from '@/api/lncomingManagement/incomingListApi';
export default {
  data () {
    // 默认表单数据
    const defaultWhere = {
      // 商家名称
      merchantName: '',
      // 所属代理
      oemName: '',
      // 进件状态
      merchanApplyForStatus: '',
      // 申请时间
      dateRange: [],
      // 进件类型
      applyForType: ''
    };
    return {
      // 表单数据
      where: { ...defaultWhere },
      subordinateAgentList: [],
      sumbitForm: {},
      pageNum: 1,
      count: 0,
      canRun: false
    };
  },
  props: {
    typeList: Array
  },
  computed: {
    // 是否开启响应式布局
    styleResponsive () {
      return this.$store.state.theme.styleResponsive;
    }
  },


  methods: {
    //  查询代理id
    async oemNameFn (status) {
      console.log(status);
      if (status) {
        const res = await oemQueryAgentList({
          limit: 10,
          oemType: 2,
          page: 1,
          parentId: this.$store.state.user.info.oemId
        });
        // 下属代理的数据
        console.log(res);
        this.count = res.data.count
        if (res.code == 2000 && res.data.list.length) {
          const agentList = res.data.list.map((item) => {
            return {
              label: item.oemCode,
              value: item.id
            };
          });

          this.subordinateAgentList = agentList;
          console.log(this.subordinateAgentList);
        }
      }
    },

    /* 搜索 */
    search () {
      for (const key in this.where) {
        if (key == 'dateRange') {
          let startTime = this.where[key]?.[0];
          let endTime = this.where[key]?.[1];
          this.sumbitForm['startTime'] = startTime == undefined ? '' : startTime;
          this.sumbitForm['endTime'] = endTime == undefined ? '' : endTime;
        } else {

          this.sumbitForm[key] = this.where[key];
        }
      }

      this.$emit('search', this.sumbitForm);
    },
    /*  重置 */
    resetForm (formName) {
      this.$refs[formName].resetFields();
      this.sumbitForm = {};
      this.where = {};
      this.$emit('search', this.sumbitForm);
    },

    //滚动条滚动到底部

    loadMore () {

      this.throttle(this.task, 1000)();
      //   this.task()
    },
    async task () {
      if (this.count > 10) {

        //页数加一
        this.pageNum++;
        // //发送网络请求
        console.log(this.pageNum);

        const res = await oemQueryAgentList({
          limit: 10,
          oemType: 2,
          page: this.pageNum,
          parentId: this.$store.state.user.info.oemId
        });
        // 下属代理的数据  
        console.log(res);
        this.count = res.data.count
        if (res.code == 2000 && res.data.list.length) {
          const agentList = res.data.list.map((item) => {
            return {
              label: item.oemCode,
              value: item.id
            };
          });
          this.subordinateAgentList = this.subordinateAgentList.concat(agentList);
          console.log(this.subordinateAgentList);
        }
        this.count = this.count - 10;
      }
    },
    throttle (fun, timeout) {
      this.canRun = true;
      return () => {
        // 如果是true 就不走这
        if (!this.canRun) return;
        this.canRun = false;
        setTimeout(() => {
          fun();
          this.canRun = true;
        }, timeout);
      };
    }
  }
};
</script>
<style lang="less" scoped>
.navList {
  margin-bottom: 25px;
  font-weight: 700;
}
</style>
